import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import dayjs from "dayjs";
import base from "@src/store/base";
import { getUserPhone } from "@src/api/base";
import { hourToMinute, minuteToHour } from "@src/common/dateUtils";

Component({
  options: {
    addGlobalClass: true
  },
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['themeColor', 'themeTitleColor'],
  }],
  properties: {
    tips: {
      type: Object
    },
    product: {
      type: Object
    },
    beginDay: {
      type: String
    },
    num: {
      type: Number,
      value: 1
    },
    themeColorRgba: {
      type: String
    },
    memberPhone: {
      type: String
    },
    inpDisabled:null
  },
  data: {
    nameList: [],
    phone: '',
    today: '',
    curTime: '',
    arrivalTime: '',
    arrivalTimeList: [],
    chooseComeToShopTime: false
  },
  observers: {
    'num': function (num) {
      let list = new Array(num);
      for (let i = 0; i < num; i++) {
        list[i] = this.data.nameList[i];
      }
      this.setData({ nameList: list });
    },
    'product': function (product) {
      if (!product) {
        return;
      }
      this.genArrivalTimeList();
    },
    'memberPhone': function (memberPhone) {
      if (!memberPhone || this.data.phone) {
        return;
      }
      this.setData({ phone: this.data.memberPhone })
    },
    'beginDay': function (beginDay) {
      if (this.data.product) { this.genArrivalTimeList() }

    },
    'inpDisabled':function(inpDisabled){
      console.log('inpDisabled==========',inpDisabled)
    }
  },
  lifetimes: {
    attached: function () {
      this.setData({
        today: dayjs().format("YYYY-MM-DD"),
        curTime: dayjs().format("HH:mm")
      })
    }
  },
  methods: {
    genArrivalTimeList() {
      let nowTime = dayjs().format("YYYY-MM-DD");
      // console.log("nowTime", nowTime)
      // console.log("nowTime==this.data.beginDay", nowTime == this.data.beginDay)
      //当前的分钟
      let min = dayjs().hour() * 60 + dayjs().minute();
      console.log('min', min)
      // 生成可选到店时间列表
      let periodStartTime = 0
      let periodEndTime = null
      let step = 60;
      //是否需要删除第一个
      let flag = false;
      if (this.data.product.productType === 0) {
        if (this.data.tips.earliestCheckTime) {
          periodStartTime = hourToMinute(this.data.tips.earliestCheckTime);
          console.log('periodStartTime', periodStartTime)
        }
        // 次日06:00
        periodEndTime = 24 * 60 + 6 * 60
        step = 60;
      } else {
        periodStartTime = this.data.product.periodStartTime
        periodEndTime = this.data.product.periodEndTime
        step = 30;
        if (!periodEndTime) {
          // 23:59
          periodEndTime = 24 * 60 - 1
        }
      }
      // console.log('periodStartTime', periodStartTime)
      // console.log('periodEndTime', periodEndTime)
      if (nowTime == this.data.beginDay && min > periodStartTime) {
        periodStartTime = min;
        flag = true;
      }
      let list = [minuteToHour(periodStartTime)];
      console.log('list', list)
      let cur = periodStartTime - periodStartTime % step + step;
      while (cur < periodEndTime) {
        list.push(minuteToHour(cur))
        cur = cur + step;
      }
      list.push(minuteToHour(periodEndTime));
      if (flag) {
        list.shift()
      }
      // list.shift()
      // this.setData({ arrivalTimeList: list, arrivalTime: list[0] });
      this.setData({ arrivalTimeList: list });
      // console.log("arrivalTimeList", this.data.arrivalTimeList)
    },
    handleNameInput(e) {
      let index = e.currentTarget.dataset.index;
      this.data.nameList[Number(index)] = e.detail.value;
      this.setData({ nameList: this.data.nameList })
    },
    handlePhoneInput(e) {
      console.info("e.detail.value", e.detail.value)
      this.setData({ phone: e.detail.value })
    },
    getPhoneNumber(e) {
      if (!e.detail.iv) {
        return;
      }
      let data = {
        ivStr: e.detail.iv,
        encryptedData: e.detail.encryptedData,
      }
      getUserPhone(data).then(res => {
        if (res.code === 0) {
          this.setData({ phone: res.data })
        } else {
          wx.showToast({ title: "手机号获取失败，请重试", icon: "none", duration: 3000 });
        }
      })
    },
    handleClose() {
      this.setData({ chooseComeToShopTime: false })
    },
    handleShowSelectTime() {
      this.setData({ chooseComeToShopTime: true })
    },
    handleSelectTime(e) {
      this.setData({ arrivalTime: e.currentTarget.dataset.time, chooseComeToShopTime: false })
    },
    handleStop() {
      return true;
    },
    check() {
      if (!this.data.phone || !/^[1][2,3,4,5,6,7,8,9][0-9]{9}$/.test(this.data.phone)) {
        wx.showToast({ title: `手机号输入有误`, icon: "none", duration: 1500 });
        return false;
      }
      let nameLen = this.data.nameList.filter(item => !!item).length;
      if (nameLen < 1 || (this.data.product.productType !== 2 && nameLen < this.data.num)) {
        wx.showToast({ title: "预订人名字不能为空", icon: "none", duration: 1500 });
        return false;
      }
      return true;
    },
    getForm() {
      let nameList = this.data.nameList
      if (this.data.product.productType === 2) {
        nameList = [nameList[0]]
      }
      return {
        checkInList: nameList,
        checkInPhone: this.data.phone,
        arrivalTime: this.data.arrivalTime,
        arrivalTimeMinute: hourToMinute(this.data.arrivalTime),
      }
    },
  }
});
